package stagefour.servlet.user;

import com.alibaba.fastjson2.JSONObject;
import stagefour.model.ResultModel;
import stagefour.model.UserModel;
import stagefour.service.UserService;
import stagefour.service.impl.UserServiceImpl;
import stagefour.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/user/add")
public class UserAddServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//      1.乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
//        2.得到前端的值
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String isadmin = req.getParameter("isadmin");

//        3.封装model
        UserModel model = new UserModel();
        model.setUsername(Tool.nullToStr(username));
        model.setPassword(Tool.nullToStr(password));

        model.setEmail(Tool.nullToStr(email));
        model.setPhone(Tool.nullToStr(phone));
        model.setIsadmin(Tool.strToInt(isadmin));
//        4.调用服务逻辑层
//        无论哪个对象都应该有相同的增删改查的操作，应该提一个父类
//        这个父类到底是普通的父类还是抽象的父类还是抽象的接口父类？

        UserService userService = new UserServiceImpl();
//        5.得到返回值:对于更新，返回的是影响的条数
        ResultModel resultModel = userService.addUser(model);
//        6.将值返回给前端
        resp.getWriter().println(JSONObject.toJSONString(resultModel));


    }
}
